package com.webfinal.backsystem.service.impl;

import com.webfinal.backsystem.dao.ProductMapper;
import com.webfinal.backsystem.dao.ShoppingCartMapper;
import com.webfinal.backsystem.entity.Product;
import com.webfinal.backsystem.entity.ShoppingCart;
import com.webfinal.backsystem.request.shoppingcart.CartOperationRequest;
import com.webfinal.backsystem.request.shoppingcart.QueryCartRequest;
import com.webfinal.backsystem.response.shoppingcart.CartOperationResponse;
import com.webfinal.backsystem.response.shoppingcart.QueryCartResponse;
import com.webfinal.backsystem.service.CartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    private static Logger LOG= LoggerFactory.getLogger(CartServiceImpl.class);
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private ProductMapper productMapper;
    @Override
    public QueryCartResponse queryCart(QueryCartRequest request) {
        QueryCartResponse response =new QueryCartResponse();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByUserId(request.getUserId());
        if (shoppingCartList!=null && shoppingCartList.size()>0) {
            response.setResultCode("0");
            response.setResultMsg("");
            response.setShoppingCartList(shoppingCartList);
        }
        else {
            response.setResultCode("0");
            response.setShoppingCartList(shoppingCartList);
            response.setResultMsg("购物车空空如也");
        }
        return response;
    }

    @Override
    public CartOperationResponse cartOperation(CartOperationRequest request) {
        CartOperationResponse response = new CartOperationResponse();
        ShoppingCart shoppingCart=new ShoppingCart();
        switch (request.getFlag()) {
            case 1:
                shoppingCart.setShoppingCartId(request.getShoppingCartId());
                shoppingCart.setProductNum(request.getNum());
                shoppingCartMapper.updateByPrimaryKey(shoppingCart);
                response.setShoppingCartList(shoppingCartMapper.selectByUserId(request.getUserId()));
                response.setResultCode("0");
                break;
            case 2:
                Product product=new Product();
                product=productMapper.selectByPrimaryKey(request.getProductId());
                shoppingCart.setImage(product.getImage());
                shoppingCart.setProductNum(request.getNum());
                shoppingCart.setPrice(product.getProductPrice());
                shoppingCart.setProductName(product.getProductName());
                shoppingCart.setUserId(request.getUserId());
                shoppingCart.setProductId(request.getProductId());
                shoppingCart.setShoppingCartId(0);
                shoppingCart.setAddDate(new Date());
                shoppingCartMapper.insert(shoppingCart);
                response.setShoppingCartList(shoppingCartMapper.selectByUserId(request.getUserId()));
                response.setResultCode("0");
                break;
            case 3:
                shoppingCartMapper.deleteByPrimaryKey(request.getShoppingCartId());
                response.setShoppingCartList(shoppingCartMapper.selectByUserId(request.getUserId()));
                response.setResultCode("0");
                break;
            case 4:
                shoppingCartMapper.deleteByUserId(request.getUserId());
                response.setResultCode("0");
                response.setResultMsg("购物车已清空");
                break;
            default:
                break;
        }
        return response;
    }
}
